<?php 
session_start();
include 'operations/connection.php';
include "generallibraries/funcs.php";
require_once('calendar/classes/tc_calendar.php');
$_SESSION['action'] = "acc-search-searchpurchase";

if(!isset($_REQUEST['page'])){
	$list["approvedoc.Tile"]="ASC";
	$list["employee.FullName"]="ASC";
	$list["approvedoc.SumMoney"]="ASC";
	$list["approvedoc.Timecreate"]="DESC";
	$str="approvedoc.Timecreate";
	$_SESSION['sortn']=$str;
	$_SESSION['sort']=$list;
}else{
	$str=$_SESSION['sortn'];
	$list=$_SESSION['sort'];
}

if(array_key_exists("page",$_GET))
    $page  = $_GET['page'];
else
    $page = 1;

$fromdate=date("Y-m-d");
$todate=date("Y-m-d");
$_REQUEST['cbcus']="ALL";
//select DISTINCT buying_document.company from buying_document where buying_document.company!=''
$hamsort=" where ac_customer.Activest=1 order by ac_customer.CustomerName desc";
$sqlCustomer = generalFunctions::listCustomer("ac_customer.CustomerID, ac_customer.CustomerName",$hamsort);
$mangCustomer=mysql_query($sqlCustomer);


$sql="select DISTINCT approvedoc.IDApproveDoc, approvedoc.IDTypeDoc, approvedoc.Tile, employee.FullName, approvedoc.Timecreate, approvedoc.SumMoney from approvedoc ,buying_document, ac_customer,employee where approvedoc.IDEmployee=employee.IDEmployee and approvedoc.IDTypeDoc in(13,15) and approvedoc.IDApproveDoc=buying_document.document_id and CONVERT(buying_document.company using utf8) = convert(ac_customer.CustomerName using utf8) ";
$mapp=mysql_query($sql);
$number_of_page = ceil ( mysql_num_rows( $mapp ) / $rows_per_page ); 

?>
<link	href="css/style.css" rel="stylesheet" type="text/css" />
<div class="title"></div>
                    <div class="content_code">
						<div class="approval-right">		
                        	<div style="font-size:18px; padding-left:30px; margin-bottom:10px; font-weight:bold; background:url(images/find.png) no-repeat left center"> Search</div>				
			<div style="border: 1px solid silver; padding: 10px 0; margin-bottom: 30px; background:white">
                                <table width="80%" style="padding:0 10px">
                                	<tr>
                                    	<td><?php echo $l_from;?></td>
                                        <td><?php echo $l_to;?></td>
                                        <td><?php echo $l_customer;?></td>
                                        <td></td>
                                        <td></td>
                                    </tr>
                                    <tr>
                                        <td>
                                        	<?php
											  $myCalendar1 = new tc_calendar("datefrom", true);
											  $myCalendar1->setIcon("calendar/images/iconCalendar.gif");
											  $d=split('-',$fromdate,3);
											 
											  $myCalendar1->setDate($d[2],$d[1],$d[0]);
											  $myCalendar1->setPath("calendar/");
											  $myCalendar1->setYearInterval(2010, 2015);
											  $myCalendar1->dateAllow('2010-01-01', '2015-12-31');
											  //$myCalendar1->setHeight(350);
											  //$myCalendar1->autoSubmit(true, "form1");
											  //$myCalendar1->setSpecificDate(array("2011-04-01", "2011-04-13", "2011-04-25"), 0, 'month');
											  $myCalendar1->setOnChange("myChanged('test')");
											  //$myCalendar->rtl = true;
											  $myCalendar1->writeScript();
						
											  //echo $myCalendar1->getDate();
											  
										  	?>
                                        </td>
                                        <td>
                                        	<?php
											  $myCalendar1 = new tc_calendar("dateto", true);
											  $myCalendar1->setIcon("calendar/images/iconCalendar.gif");
											  $d=split('-',$todate,3);
											 
											  $myCalendar1->setDate($d[2],$d[1],$d[0]);
											  $myCalendar1->setPath("calendar/");
											  $myCalendar1->setYearInterval(2010, 2015);
											  $myCalendar1->dateAllow('2010-01-01', '2015-12-31');
											  //$myCalendar1->setHeight(350);
											  //$myCalendar1->autoSubmit(true, "form1");
											  //$myCalendar1->setSpecificDate(array("2011-04-01", "2011-04-13", "2011-04-25"), 0, 'month');
											  $myCalendar1->setOnChange("myChanged('test')");
											  //$myCalendar->rtl = true;
											  $myCalendar1->writeScript();
						
											  //echo $myCalendar1->getDate();
											  
										  	?>
                                        </td>
                                        <td>
                                        	<select name="cbcus" style="width:150px; border:1px solid silver; height:22px">
                                        		<option value="ALL"><?php echo $l_all;?></option>
                                        	<?php 
                                        		if(mysql_num_rows($mangCustomer)>0){
                                        			while($row=mysql_fetch_array($mangCustomer)){
                                        			?>
                                        			<option value="<?php echo $row[CustomerID]."*****".$row[CustomerName];?>"
                                        				<?php 
                                        					if($_REQUEST['cbcus']==$row[CustomerID]){
                                        						?>
                                        							selected="selected"
                                        						<?php                                         					}
                                        				?>
                                        			>
                                        				<?php echo $row[CustomerName]?>
                                        			</option>
                                        			<?php 
                                        			}
                                        		}
                                        	?>
                                        	</select>
                                        </td>
                                        <td></td>
                                        <td><input type="button" name="Search" class="btnupbg btnupload" value="<?php echo $l_search;?>" /></td>
                                    </tr>
                                </table>
							</div>
                            <div style="font-size:18px; padding-left:10px; margin-bottom:10px; font-weight:bold;"> <?php echo $l_Purchaseorder;?> : <?php echo mysql_num_rows($mapp);?></div>
                            <table class="rf-dt" width="100%">
                                <thead>
                                    <tr>
                                      <th width="5%" class="rf-dt-shdr-c" style="padding:10px" scope="col"><?php echo $l_no;?></th>
                                      <th width="45%" class="rf-dt-shdr-c" style="padding:10px" scope="col"><a href="" style="color:#1f71ee"><?php echo $l_title;?></a></th>
                                        <th width="10%" class="rf-dt-shdr-c" scope="col"><a href="" style="color:#1f71ee"><?php echo $l_total;?></a></th>
                                        <th width="25%" class="rf-dt-shdr-c" scope="col"><a href="" style="color:#1f71ee"><?php echo $l_writer;?></a></th>
                                        <th width="15%" class="rf-dt-shdr-c" scope="col"><a href="" style="color:#1f71ee"><?php echo $l_created_date;?></a></th>
                                    </tr>
                                </thead>
                                <tbody>
                                	<?php 
                                		if(mysql_num_rows($mapp)>0){
                                			$i=0;
                                			while($row = mysql_fetch_array($mapp)){
                                				if ( $i >= $page_start ){
                                				?>
                                				<tr>
			                                      <td class="rf-dt-c"><?=$i+1?></td>
			                                        <td class="rf-dt-c"><a href="accountant-search.php?id=<?=$row[IDApproveDoc]?>&type=<?=$row[IDTypeDoc]?>" style="color:#0078D0"><?php echo $row[Tile]?></a></td>
			                                        <td class="rf-dt-c" ><?php echo number_format($row[SumMoney])?></td>
			                                        <td class="rf-dt-c"><?php echo $row[FullName]?></td>
			                                        <td class="rf-dt-c"><?php echo $row[Timecreate]?></td>
			                                    </tr>
                                				<?php 
												}
                                			$i++;
											if ($i >= $page_end){ 
												break; 
											}
                                			}
                                		}
                                		
                                	?>
                                </tbody>
                            </table>
					  </div>
					</div>